// 权限路由(需登录)
import 'package:app/common/index.dart';
import 'package:app/views/home.dart';
import 'package:app/views/project/create_or_edit_project.dart';
import 'package:app/views/project/project.dart';
import 'package:app/views/user/edit_info.dart';
import 'package:app/views/user/edit_account.dart';
import 'package:app/views/user/theme_setting.dart';
import 'package:app/views/user/user_info.dart';
import 'package:app/views/user/verification_code.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

// 权限路由重定向判断方法
String? authRouteRedirectHelper(BuildContext context) {
  bool isLogin = UserHelper.checkLogin(context);
  if (isLogin) {
    // return "/home/project";
    // return "/home/project/create_project";
    return null;
  }
  return "/login";
}

final List<GoRoute> authRoutes = [
  GoRoute(
    path: "/home",
    redirect: (context, state) => authRouteRedirectHelper(context),
    builder: (context, state) => HomePage(),
    routes: [
      // 用户
      GoRoute(
          path: "/user_info",
          builder: (context, state) => UserInfoPage(),
          routes: [
            GoRoute(
              path: "/edit_info/:action_type/:data",
              builder: (context, state) => EditInfoPage(
                data: state.pathParameters["data"],
                actionType: state.pathParameters["action_type"] == "name"
                    ? EditInfoPageActionType.name
                    : state.pathParameters["action_type"] == "position"
                        ? EditInfoPageActionType.position
                        : EditInfoPageActionType.enterprise,
              ),
            ),
            GoRoute(
                path: "/edit_account/:action_type",
                builder: (context, state) => EditAccountPage(
                      actionType: state.pathParameters["action_type"] == "phone"
                          ? EditAccountPageActionType.phone
                          : EditAccountPageActionType.email,
                    )),
            GoRoute(
              path: "/verification_code/:action_type/:account",
              builder: (context, state) => VerificationCodePage(
                actionType: state.pathParameters["action_type"] == "phone"
                    ? VerificationCodePageActionType.phone
                    : VerificationCodePageActionType.email,
                account: state.pathParameters["account"]!,
              ),
            ),
          ]),
      // 项目
      GoRoute(
          path: "/project",
          builder: (context, state) => ProjectPage(),
          routes: [
            GoRoute(
              path: "/create_project",
              builder: (context, state) => CreateOrEditProjectPage(),
            ),
          ]),
    ],
  ),
  GoRoute(
    path: "/theme_setting",
    builder: (context, state) => ThemeSettingPage(),
  )
];
